#Christopher Culver
#Chinese Investment in Africa
#Updated: 3/17/2020

setwd("C:/Users/chris/Dropbox/Chinese Investment in Africa")

library(foreign)
Data <- read.csv("Full Data Set.csv")
attach(Data)

library(plm)
library(car)
library(stargazer)

# Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
# R package version 5.2.2. https://CRAN.R-project.org/package=stargazer 

Outlier <- Data[which(Data$ChineseShareFlow > 2.5 & Data$CountryYear!='Niger 2005' & Data$CountryYear!='Sudan 2012'),]
Other <- Data[which(Data$CountryYear=='Niger 2005' | Data$CountryYear=='Sudan 2012'),]
plot(Corruption, ChineseShareFlow, main = "Figure 1", ylab = "Chinese Share of Total FDI Flows", yaxt="n")
   axis(2, at=pretty(ChineseShareFlow), lab=paste0(pretty(ChineseShareFlow) * 100, " %"))
text(Outlier$Corruption,Outlier$ChineseShareFlow, labels = Outlier$CountryYear, pos = 3, cex = 0.7)
text(Other$Corruption,Other$ChineseShareFlow, labels = Other$CountryYear, pos = 1, cex = 0.7)



LogGDPpc2 <- log(GDPpc2)
LogGDPpc3 <- log(GDPpc3)
LogGDPpc5 <- log(GDPpc5)
SqrtNatrlResource2 <- sqrt(NaturalResource2)
SqrtNatrlResource3 <- sqrt(NaturalResource3)
SqrtNatrlResource5 <- sqrt(NaturalResource5)
LogTrade2 <- log(Trade2)
LogTrade3 <- log(Trade3)
LogTrade5 <- log(Trade5)

#Table 1

Model1a <- plm(Corruption2 ~ ChineseFlow2 + DevelopedFlow2 + LogGDPpc2 + SqrtNatrlResource2 + 
    LogTrade2 + Democracy2 + CorruptionBase2,
    data=Data,index=c("CountryFix", "Year"), model="random")

Model1b <- plm(Corruption3 ~ ChineseFlow3 + DevelopedFlow3 + LogGDPpc3 + SqrtNatrlResource3 + 
    LogTrade3 + Democracy3 + CorruptionBase3,
    data=Data,index=c("CountryFix", "Year"), model="random")

Model1c <- plm(Corruption5 ~ ChineseFlow5 + DevelopedFlow5 + LogGDPpc5 + SqrtNatrlResource5 + 
    LogTrade5 + Democracy5 + CorruptionBase5,
    data=Data,index=c("CountryFix", "Year"), model="random")

stargazer(Model1a, Model1b, Model1c, style="apsr", no.space=TRUE, column.labels = c("2 Year","3 Year","5 Year"), 
    model.numbers = FALSE, title = "Change in Corruption", dep.var.labels.include = FALSE)


#Table 2


Model2c <- plm(ChineseShareFlow ~ CorruptionAvg5 + log(GDPpc5)+ sqrt(NaturalResource5) + 
      Democracy5 + log(Population) + PctProtest + PctMuslim + CommonLaw,
      data=Data,index=c("CountryFix", "Year"), model="random")
Model2b <- plm(ChineseShareFlow ~ CorruptionAvg3 + log(GDPpc3)+ sqrt(NaturalResource3) + 
      Democracy3 + log(Population) + PctProtest + PctMuslim + CommonLaw,
      data=Data,index=c("CountryFix", "Year"), model="random")
Model2a <- plm(ChineseShareFlow ~ CorruptionAvg2 + log(GDPpc2)+ sqrt(NaturalResource2) + 
      Democracy2 + log(Population) + PctProtest + PctMuslim + CommonLaw,
      data=Data,index=c("CountryFix", "Year"), model="random")

stargazer(Model2a, Model2b, Model2c, style="apsr", no.space=TRUE, column.labels = c("2 Year","3 Year","5 Year"), 
    model.numbers = FALSE, title = "Chinese Share of Total FDI Flows", dep.var.labels.include = FALSE)


#Table 3

Model3a <- plm(ChineseFlow ~ CorruptionAvg5 + log(GDPpc5)+ sqrt(NaturalResource5) + 
      Democracy5 + log(Population) + PctProtest + PctMuslim + CommonLaw,
      data=Data,index=c("CountryFix", "Year"), model="random")
Model3b <- plm(DevelopedFlow ~ CorruptionAvg5 + log(GDPpc5)+ sqrt(NaturalResource5) + 
      Democracy5 + log(Population) + PctProtest + PctMuslim + CommonLaw,
      data=Data,index=c("CountryFix", "Year"), model="random")


stargazer(Model3a, Model3b, style="apsr", no.space=TRUE, column.labels = c("Chinese FDI","Developed FDI"), 
    model.numbers = FALSE, title = "5 Year FDI Flows", dep.var.labels.include = FALSE)


#### Appendix A ####

#Figure 2
plot(NaturalResource, Corruption, xlab = "Natural Resource rents (% of GDP)", main = "Figure 2")

#Figure 3 
library(gplots)
plotmeans(Corruption ~ Year, main="Figure 3\nMean Corruption Scores and Variance Across Time")

#Figure 4
plotmeans(Corruption ~ CountryCode , xaxt=n,xlab="", main = "Figure 4\nMean Corruption and Variance Across Country",
      ylab="Corruption")
	axis(1, las=2, at= seq(1,52,1), labels = levels(CountryCode))

#Figure 5
layout(matrix(1:4, ncol = 2), widths = 1, heights = 1, respect = FALSE)
par(mar = c(4,2,6,2))
plot(density(na.omit(Corruption)), main = "Corruption", xlab = "N = 572")
plot(density(na.omit(Corruption3)), main = "3 Year Change in Corruption", xlab = "N = 416")
plot(density(na.omit(Corruption2)), main = "2 Year Change in Corruption", xlab = "N = 468")
plot(density(na.omit(Corruption5)), main = "5 Year Change in Corruption", xlab = "N = 312")
title("Figure 5", outer = TRUE, line = -2, cex.main = 2)
par(def.par)

#### Appendix B ####

SummaryData <- subset(Data, select = c("Corruption", "ChineseFlow",
   "TotalFlow","DevelopedFlow", "GDPpc", 
   "ChineseShareFlow","ResourceHost", "NaturalResource",
   "EthnicFract","CommonLaw","Polity","Democracy","Trade", "Oil",
   "PctProtest","PctCatholic","PctMuslim"))
summary(SummaryData)

#### Appendix D ####
#Table 4
Model4a <- plm(Corruption2 ~ ChineseFlow2 + DevelopedFlow2 + LogGDPpc2 +  
    LogTrade2 + Polity2 + CorruptionBase2 + Oil + EthnicFract + PctProtest + PctMuslim + CommonLaw + NorthAfrica,
    data=Data,index=c("CountryFix", "Year"), model="random")

Model4b <- plm(Corruption3 ~ ChineseFlow3 + DevelopedFlow3 + LogGDPpc3 +  
    LogTrade3 + Polity3 + CorruptionBase3 + Oil + EthnicFract + PctProtest + PctMuslim + CommonLaw + NorthAfrica,
    data=Data,index=c("CountryFix", "Year"), model="random")

Model4c <- plm(Corruption5 ~ ChineseFlow5 + DevelopedFlow5 + LogGDPpc5 +  
    LogTrade5 + Polity5 + CorruptionBase5 + Oil + EthnicFract + PctProtest + PctMuslim + CommonLaw + NorthAfrica,
    data=Data,index=c("CountryFix", "Year"), model="random")

stargazer(Model4a, Model4b, Model4c, style="apsr", no.space=TRUE, column.labels = c("2 Year","3 Year","5 Year"), 
    model.numbers = FALSE, title = "Change in Corruption", dep.var.labels.include = FALSE)

#Tables 5, 6, 7

Model5 <- plm(Corruption2 ~ ChineseFlow2 + DevelopedFlow2 + LogGDPpc2 + SqrtNatrlResource2 + 
    LogTrade2 + Democracy2 + CorruptionBase2 + ResourceHost +
    ChineseFlow2*LogGDPpc2 + DevelopedFlow2 *LogGDPpc2 +
    ChineseFlow2*ResourceHost + DevelopedFlow2 *SqrtNatrlResource2,
    data=Data,index=c("CountryFix", "Year"), model="random")

Model6 <- plm(Corruption3 ~ ChineseFlow3 + DevelopedFlow3 + LogGDPpc3 + SqrtNatrlResource3 + 
    LogTrade3 + Democracy3 + CorruptionBase3 + ResourceHost +
    ChineseFlow3*LogGDPpc3 + DevelopedFlow3*LogGDPpc3 +
    ChineseFlow3*ResourceHost + DevelopedFlow3*SqrtNatrlResource3,
    data=Data,index=c("CountryFix", "Year"), model="random")

Model7 <- plm(Corruption5 ~ ChineseFlow5 + DevelopedFlow5 + LogGDPpc5 + SqrtNatrlResource5 + 
    LogTrade5 + Democracy5 + CorruptionBase5 + ResourceHost +
    ChineseFlow5*LogGDPpc5 + DevelopedFlow5*LogGDPpc5 +
    ChineseFlow5*ResourceHost + DevelopedFlow5*SqrtNatrlResource5,
    data=Data,index=c("CountryFix", "Year"), model="random")


stargazer(Model5, style="apsr", no.space=TRUE,  
    model.numbers = FALSE, title = "Change in Corruption: 2 year", dep.var.labels.include = FALSE)
stargazer(Model6, style="apsr", no.space=TRUE,  
    model.numbers = FALSE, title = "Change in Corruption: 3 year", dep.var.labels.include = FALSE)
stargazer(Model7, style="apsr", no.space=TRUE,  
    model.numbers = FALSE, title = "Change in Corruption: 5 year", dep.var.labels.include = FALSE)
